package com.vision.util.http.param;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhongchao
 * @title
 * @date 2017-08-22
 * @since v1.0.0
 */
public class GetParams extends HttpRequestParams<GetParams> {


    public GetParams(String url, String charset) {
        super(url, charset);
    }

    @Override
    public HttpRequestBase build() throws IOException {
        HttpGet httpRequestBase = new HttpGet(url);
        headers.forEach(httpRequestBase::addHeader);
        List<NameValuePair> pairs = new ArrayList<>(params.size());
        params.forEach((k, v) -> pairs.add(new BasicNameValuePair(k, v)));
        url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
        return new HttpGet(url);
    }
}
